<?php
	class home extends BaseController{
		public function getData($in_params){
			if (isset($_REQUEST["registered"]) && $_REQUEST["registered"] == 1){
				$this->template_data["message"] = "Registrácia prebehla úspešne.";
			}	
			return;
		}
		
		public function action_login($in_params = array()){
			$this->template_data["login"] = "Neznáma chyba";
			$user = new m_user();
			$user->select("", array("password" => md5($in_params["password"]), "username" => $in_params["username"]));
			if ($user->id){
				$_SESSION["user"] = $user->to_array();
			    $this->template_data["login"] = "Prihlásenie úspešné";
				if ($user->security_level == SECURITY_LEVEL_USER){
					general::redirect("plan");
				} else if($user->security_level >= SECURITY_LEVEL_ADMIN) {
					general::redirect("order_administration");
				}
			} else {
				$this->template_data["login"] = "Zlé meno alebo heslo";
			}
			return;
		}
		
		public function action_logout($in_params = array()){
			$memcache = new myMemcache();
			
			if(!isset($_SESSION["user"])){
				return;
			}
			
			$user_tag = "user_".$_SESSION["user"]["id"];
			$user_reserved_seats = isset($_SESSION["reserved_seats"])?$_SESSION["reserved_seats"]:array();
			
			foreach ($user_reserved_seats as $value) {
				if(($seat = $memcache->get($value)) && $seat["user"] == $user_tag){
					$memcache->delete($value);
					$memcache->decrement($user_tag);
				}
			}
			unset($_SESSION["reserved_seats"]);
			session_unset();
			session_destroy();
			
			$this->template_data["message"] = "Boli ste úspešne odhlásený zo systému.";
			
		}
	}
?>